package cn.itsource.org.controller;

import cn.itsource.annotation.PreAuthorize;
import cn.itsource.org.service.IMenuService;
import cn.itsource.org.domain.Menu;
import cn.itsource.org.query.MenuQuery;
import cn.itsource.common.util.AjaxResult;
import cn.itsource.common.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    public IMenuService menuService;

    /**
     * 接口：添加或修改
     * @param menu  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    @PreAuthorize(name = "添加菜单接口",sn = "menu:add")
    public AjaxResult addOrUpdate(@RequestBody Menu menu){ 
        if( menu.getId()!=null){
		menuService.update(menu);
        }else{
                menuService.save(menu);
	}
        return new AjaxResult();
       
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @PreAuthorize(name = "删除菜单接口",sn = "menu:del")
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        menuService.deleteById(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PreAuthorize(name = "批量删除菜单接口",sn = "menu:patch:del")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        menuService.batchDelete(ids);
        return new AjaxResult();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    @PreAuthorize(name = "根据菜单id查询接口",sn = "menu:findOne")
    public Menu findOne(@PathVariable("id")Long id) {
        return menuService.findById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping("/findAll")
    @PreAuthorize(name = "菜单查询全部接口",sn = "menu:findAll")
    public List<Menu> findAll(){
        return menuService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @PreAuthorize(name = "菜单分页查询接口",sn = "menu:list")
    public PageList<Menu> queryPage(@RequestBody MenuQuery query) {
        return menuService.findPageList(query);
    }

    @PreAuthorize(name = "菜单树接口",sn = "menu:tree")
    @GetMapping("/menuTree")
    public List<Menu> menuTree(){
        return menuService.menuTree();
    }
}
